package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Question;
import com.gxa.learning.core.pojo.QuestionBank;
import com.gxa.learning.pc.service.QuestionBankService;
import com.gxa.learning.pc.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 操作题库的接口
 *
 * @Author ChenJ
 * @date 2021/12/02 16:11
 **/
@RestController
@RequestMapping("/QuestionBank")
@Api(value = "QuestionBankController", tags = "操作题库的接口")
public class QuestionBankController {

    @Autowired
    private QuestionBankService questionBankService;

    @Autowired
    private QuestionService questionService;

    @PostMapping("/insert")
    @ApiOperation("添加题库的接口")
    @ApiImplicitParam(name = "questionBank", value = "questionBank", paramType = "body", dataType = "List")
    public Map<String, Object> insert(@RequestBody Map<String, Object> questionBanks) throws BusinessException, ParseException {

        // 数据校验
        if (questionBanks == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }

        QuestionBank questionBank = new QuestionBank();
        if (questionBanks.get("type") != null) {
            questionBank.setType(questionBanks.get("type").toString());
        }
        if (questionBanks.get("name") != null) {
            questionBank.setName(questionBanks.get("name").toString());
        }
        if (questionBanks.get("admin") != null) {
            questionBank.setAdmin(questionBanks.get("admin").toString());
        }
        if (questionBanks.get("status") != null) {
            questionBank.setStatus(Integer.valueOf(questionBanks.get("status").toString()));
        }
        if (questionBanks.get("courseId") != null) {
            questionBank.setCourseId(Long.valueOf(questionBanks.get("courseId").toString()));
        }
        if (questionBanks.get("queCount") != null) {
            questionBank.setQueCount(Integer.valueOf(questionBanks.get("queCount").toString()));
        }
        if (questionBanks.get("time") != null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            questionBank.setTime(format.parse(questionBanks.get("time").toString()));
        }
//        questionBank.setTime(new Date());

        Object question = questionBanks.get("question");
        List<Map<String, Object>> question1 = (List) question;
        Object o = question1.get(0);

        // 将题目数量传递到题库
        questionBank.setQueCount(question1.size());
        QuestionBank insert = questionBankService.insert(questionBank);

        // 遍历添加题目
        for (Map<String, Object> questionOne : question1) {
            System.out.println("questionOne:" + questionOne);
            questionOne.put("bankId", insert.getId());
            Question question2 = new Question();
            question2.setQuestionBankId(insert.getId());

            if (questionOne.get("answer") != null) {
                question2.setAnswer(questionOne.get("answer").toString());
            }
            if (questionOne.get("core") != null) {
                question2.setCore(Integer.valueOf(questionOne.get("core").toString()));
            }
            if (questionOne.get("item1") != null) {
                question2.setItem1(questionOne.get("item1").toString());
            }
            if (questionOne.get("item2") != null) {
                question2.setItem2(questionOne.get("item2").toString());
            }
            if (questionOne.get("item3") != null) {
                question2.setItem3(questionOne.get("item3").toString());
            }
            if (questionOne.get("item4") != null) {
                question2.setItem4(questionOne.get("item4").toString());
            }
            if (questionOne.get("lId") != null) {
                question2.setLId(Long.valueOf(questionOne.get("lId").toString()));
            }
            if (questionOne.get("title") != null) {
                question2.setTitle(questionOne.get("title").toString());
            }
            if (questionOne.get("type") != null) {
                question2.setType(Integer.valueOf(questionOne.get("type").toString()));
            }

            questionService.insert(question2);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/select")
    @ApiOperation("获取题库列表的接口（包含模糊查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", dataType = "Integer", required = false, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", dataType = "Integer", required = false, paramType = "query"),
            @ApiImplicitParam(name = "questionBank", value = "questionBank", dataType = "QuestionBank", paramType = "body", dataTypeClass = QuestionBank.class)
    })
    public Map<String, Object> select(@RequestParam(name = "page", required = false) Integer page,
                                      @RequestParam(name = "size", required = false) Integer size,
                                      @RequestBody QuestionBank questionBank) throws IllegalAccessException, BusinessException {

        // 先进行数据校验
        if (questionBank == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }

        PageInfo<QuestionBank> questionBankPageInfo = questionBankService.selectByPage(page, size, questionBank);
        List<QuestionBank> list = questionBankPageInfo.getList();
        // 如果查出没有数据则抛出没有数据异常
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        long total = questionBankPageInfo.getTotal();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        jsonResult.put("count", total);
        return jsonResult.getDataMap();
    }


    @PostMapping("/update")
    @ApiOperation("修改题库的接口")
    @ApiImplicitParam(name = "questionBank", value = "questionBank", dataType = "QuestionBank", paramType = "body", dataTypeClass = QuestionBank.class)
    public Map<String, Object> update(@RequestBody QuestionBank questionBank) throws BusinessException {

        if (questionBank.getId() == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }

        Integer integer = questionBankService.updateById(questionBank);
        if (integer == 0) {
            throw new BusinessException(ExceptionCode.WRONG_UPLOAD);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除题库的接口")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Map", paramType = "body")
    @Transactional
    public Map<String, Object> delete(@RequestBody Map<String, Object> id) throws BusinessException {

        if (id == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }


        // 删除题库操作
        Integer integer = questionBankService.deleteById(Long.valueOf(id.get("id").toString()));

        Question question = new Question();
        question.setQuestionBankId(Long.valueOf(id.get("id").toString()));

        // 遍历删除题库中题目的操作
        List<Question> select = questionService.select(question);
        for (Question findQuestion : select) {
            questionService.deleteById(findQuestion.getId());
        }

        if (integer == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DELETE);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
